﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Goods;
using Coldairarrow.Entity.PersonalCenter;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PersonalCenter
{
    public class BTB_browseBusiness : BaseBusiness<BTB_browse>, IBTB_browseBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_browseBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<BTB_GoodsDto>> GetDataListAsync(PageInput<BTB_GoodsDto> input)
        {
            var browse = input.Search;
            var result = GetIQueryable().Where(b => b.userid == _operator.UserId);
            var where = LinqHelper.True<BTB_Goods>();
            int count = result.Count();
            var aa = Db.GetIQueryable<BTB_browse>();
            //品牌
            if (browse.StartTime.HasValue && browse.EndTime.HasValue)
            {
                result = result.Where(k => k.CreateTime >= browse.StartTime && k.CreateTime <= browse.EndTime);
            }

            //排除删除的商品浏览记录
            result = from a in result
                     join b in Db.GetIQueryable<BTB_Goods>() on a.Goodsid equals b.ID
                     select a;

            var q = await result.Select(s => new BTB_GoodsDto
            {
                BId = s.id,
                ID = s.Goodsid,
                GoodsName = Db.GetIQueryable<BTB_Goods>(true).Where(v=>v.ID == s.Goodsid).FirstOrDefault().GoodsName,
                GoodsDescribe = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().GoodsDescribe,
                GoodsPic = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().GoodsPic,
                IsPointGoods = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().IsPointGoods,
                GoodsPrices = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(z => z.GoodsID == s.Goodsid).FirstOrDefault().Price,
                PointPrice = Db.GetIQueryable<BTB_Goods>(true).Where(v => v.ID == s.Goodsid).FirstOrDefault().PointPrice
            }).OrderByDescending(x => x.BId).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_GoodsDto> { Data = q, Total = count };
        }

        public List<GoodsPrice> HotSale()
        {
            var browse = GetIQueryable();
            var goods = Db.GetIQueryable<BTB_Goods>();
            var price = Db.GetIQueryable<BTB_GoodsSpecificationsItem>();
            var e = browse
                    .GroupBy(x => x.Goodsid, (key, result) => new { Id = key, Count = result.Count() })
                    .OrderBy("Count", "DESC")
                    .Take(4)
                    .Select(x => new GoodsPrice { Goods = goods.Where(y => y.ID == x.Id).First(), Price = price.Where(y => y.GoodsID == x.Id).FirstOrDefault() });

            return e.ToList();
        }
        /// <summary>
        /// 单独添加
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task AddDataAsync(BTB_browse data)
        {
            BTB_browse data1 = new BTB_browse()
            {
                id = GuidHelper.GenerateKey(),
                userid = _operator.UserId,
                Goodsid = data.Goodsid,
                CreatorId = data.CreatorId,
                Deleted = data.Deleted,
                CreateTime = data.CreateTime,
                Editor = data.Editor,
                EditTime = data.EditTime
            };
            var count = GetIQueryable().Where(q => q.userid == _operator.UserId && q.Goodsid == data1.Goodsid).Count();
            if (count > 0)
            {
                throw new BusException("已经存在", errorCode: -1);
            }
            await InsertAsync(data1);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public async Task UpdateDataAsync(BTB_browse data)
        {
            await UpdateAsync(data);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task DeleteDataAsync(List<string> ids)
        {
            foreach (var item in ids)
            {
                var data = GetIQueryable().Where(q => q.userid == _operator.UserId && q.Goodsid == item).FirstOrDefault().id;
                await DeleteAsync(data);
            }
        }
      
        #endregion

        #region 私有成员

        #endregion
    }
}